package com.bw.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import com.bw.entity.OrderInfo;
import com.bw.mapper.OrderInfoMapper;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Transactional //添加Transactional事务处理
    @Override
    public OrderInfo add(OrderInfo orderInfo) {
        //用户账号、来源、支付方式非空校验

        Assert.notNull(orderInfo.getUserId(),"用户账号不为空");
        Assert.notNull(orderInfo.getSourceId(),"来源不为空");
        Assert.notBlank(orderInfo.getPayType(),"支付方式不为空");
        //订单编号自动生成，不能重复
        String orderNum = UUID.randomUUID().toString();
        orderInfo.setOrderNum(orderNum);

        //订单状态，默认为：“未支付”
        Integer status = orderInfo.getStatus();
        if (status == null){
            orderInfo.setStatus(0); //建表时定义好了 0是 未支付
        }
        // 打印方法名称和参数到控制台
        System.out.println("add的入参是 " + orderInfo);

        // 订单入库成功
        orderInfoMapper.add(orderInfo);
       // service 要返回订单信息 mapper不能返回
        return orderInfo;
    }

    @Override
    public List<OrderInfo> selectList(OrderInfo orderInfo) {



        return orderInfoMapper.selectList(orderInfo);
    }
}
